/*
 * Copyright 2021 MindMotion Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef __BOARD_INIT_H__
#define __BOARD_INIT_H__

#include <stdio.h>
#include <stdint.h>

#include "hal_common.h"
#include "hal_rcc.h"
#include "hal_uart.h"
#include "hal_fsmc.h"
#include "hal_gpio.h"

#include "clock_init.h"
#include "pin_init.h"

/* app name. */
#define BOARD_APP_NAME                  "project.bin"

/* QSPI. */
#define BOARD_QSPI_SCK_DIV              4u
#define BOARD_QSPI_RX_DELAY_CYCLES      1u
#define BOARD_QSPI_CS_HIGH_LEVEL_TIME   2u

/* cmd. */
#define BOARD_EXT_FLASH_CMD_BUS_WIDTH   QSPI_BusWidth_1b
#define BOARD_EXT_FLASH_CMD_VALUE       0x6B /* fast read quad. */
#define BOARD_EXT_FLASH_ADDR_BUS_WIDTH  QSPI_BusWidth_1b
#define BOARD_EXT_FLASH_ADDR_WORD_WIDTH QSPI_WordWidth_24b
#define BOARD_EXT_FLASH_DUMMY_CYCLES    8u
#define BOARD_EXT_FLASH_DATA_BUS_WIDTH  QSPI_BusWidth_4b

/* USB. */
#define BOARD_USB_PORT               USB
#define BOARD_USB_IRQHandler         USB_FS_IRQHandler
#define BOARD_USB_IRQn               USB_FS_IRQn

/* button. */
#define BOARD_BTN_PORT              GPIOA
#define BOARD_BTN_PIN               GPIO_PIN_0

/* SOFT SPI. */
#define BOARD_SOFTSPI_DELAY_CNT         100
#define BOARD_SOFTSPI_NSS_GPIO_PORT     GPIOF
#define BOARD_SOFTSPI_NSS_GPIO_PIN      GPIO_PIN_6
#define BOARD_SOFTSPI_SCK_GPIO_PORT     GPIOG
#define BOARD_SOFTSPI_SCK_GPIO_PIN      GPIO_PIN_7
#define BOARD_SOFTSPI_MOSI_GPIO_PORT    GPIOG
#define BOARD_SOFTSPI_MOSI_GPIO_PIN     GPIO_PIN_6
#define BOARD_SOFTSPI_MISO_GPIO_PORT    GPIOF
#define BOARD_SOFTSPI_MISO_GPIO_PIN     GPIO_PIN_8

#define BOARD_SPIFLASH_WRITE_TIMEOUT    1000

#define BOARD_APP_BASE                  0x90000000
#define BOARD_MD5_BASE                  0x900FF000
#define BOARD_MEM_BASE                  0x90100000

#define BOARD_FLASH_SIZE                (2 * 1024 * 1024) /* 2MB. */

void BOARD_Init(void);
void BOARD_EraseSector4KB(uint32_t base);
void BOARD_WriteSector4KB(uint32_t base, uint8_t * buf);



#endif /* __BOARD_INIT_H__ */
